package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.commonutils.results;
import com.entity.pojo.Categories;
import com.entity.vo.CourseOverviewVo;
import com.mapper.CategoriesMapper;
import com.service.CategoriesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chenmo
 * @since 2022-04-02
 */
@Service
public class CategoriesServiceImpl extends ServiceImpl<CategoriesMapper, Categories> implements CategoriesService {

    @Override
    public results updateCategoryPosition(Integer newIndex, Categories currRow) {
        Integer oldPosition = currRow.getPositionId();
        if (newIndex > oldPosition) {
            // 向下拖动，需要更新位置值的记录，位置值全体加一
            System.out.println("-----------------向下拖动");
            downSwap(newIndex,currRow);
        } else if (newIndex < oldPosition) {
            // 向上拖动，需要更新位置值的记录，位置值全体减一
            System.out.println("---------------------向上拖动");
            upSwap(newIndex,currRow);
        }
        // 最后更新移动的数据
        currRow.setPositionId(newIndex);
        baseMapper.updateById(currRow);
        return results.ok();
    }

    @Override
    public List<CourseOverviewVo> getCourseOverview(long current, long limit, CourseOverviewVo courseOverviewQuery) {
        Page<CourseOverviewVo> pageCondition = new Page<>(current, limit);
        QueryWrapper<CourseOverviewVo> wrapper = new QueryWrapper<>();
        validateCondition(wrapper, courseOverviewQuery);
        wrapper.groupBy("courseId").orderByAsc("courseCode");
        return baseMapper.getCourseOverview(pageCondition,wrapper);
    }

    private void validateCondition(QueryWrapper<CourseOverviewVo> wrapper, CourseOverviewVo courseOverviewQuery) {
        String courseCode = courseOverviewQuery.getCourseCode();
        String courseName = courseOverviewQuery.getCourseName();

        if (!StringUtils.isEmpty(courseCode))
            wrapper.like("categories.courseCode ", courseCode);
        if (!StringUtils.isEmpty(courseName))
            wrapper.like("courses.courseName ", courseName);
    }

    private void upSwap(Integer newIndex, Categories currRow) {
        Integer oldPosition = currRow.getPositionId();
        Integer courseId = currRow.getCourseId();
        QueryWrapper<Categories> wrapper = new QueryWrapper<>();
        wrapper.eq("courseId", courseId);
        // 小于oldPosition
        wrapper.lt("positionId", oldPosition);
        // 大于等于newIndex
        wrapper.ge("positionId", newIndex);
        List<Categories> categoryList = baseMapper.selectList(wrapper);
        categoryList.forEach(item -> {
            item.setPositionId(item.getPositionId() + 1);
            baseMapper.updateById(item);
        });
    }

    private void downSwap(Integer newIndex, Categories currRow) {
        Integer oldPosition = currRow.getPositionId();
        Integer courseId = currRow.getCourseId();
        QueryWrapper<Categories> wrapper = new QueryWrapper<>();
        wrapper.eq("courseId", courseId);
        // 大于oldPosition
        wrapper.gt("positionId", oldPosition);
        // 小于等于newIndex
        wrapper.le("positionId", newIndex);
        List<Categories> categoryList = baseMapper.selectList(wrapper);
        categoryList.forEach(item -> {
            item.setPositionId(item.getPositionId() - 1);
            baseMapper.updateById(item);
        });
    }
}
